import 'package:flutter/foundation.dart';
import 'package:loxen/api/api.dart';
import 'package:loxen/mode/base_mode.dart';
import 'package:loxen/mode/comment_mode.dart';
import 'package:loxen/mode/mine/mine_comment_list.dart';
import 'package:rxdart/rxdart.dart';

mixin UserCommentMixin {
  Stream<int> deleteComment(String contentType, String typeId, String id) {
    return ApiService.deleteComment(contentType, typeId, id)
        .flatMap((value) => Stream.value(value.statusCode));
  }

  Stream<Comment> commentToSB(String id, String content,
      {String parent, String previousComment}) {
    return ApiService.commentInspiration(id, content, parent, previousComment)
        .flatMap((value) => compute(decodeSendComment, value.data).asStream())
        .flatMap((value) => Stream.value(value.data));
  }

  Stream<MineCommentList> userComment(
      {String userId = '0', String page = '1', String pageSize}) {
    return ApiService.userComments(userId, page, pageSize)
        .flatMap(
            (value) => compute(decodeMineCommentData, value.data).asStream())
        .flatMap((value) => Stream.value(value.data));
  }

  static BaseMode<Comment> decodeSendComment(dynamic json) {
    return BaseMode<Comment>(json);
  }

  static BaseMode<MineCommentList> decodeMineCommentData(dynamic json) {
    return BaseMode<MineCommentList>(json);
  }
}
